﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MazeModel;

namespace MazeConsole
{
    class ConsoleDrawer
    {
        private bool LevelComplete = false;
        private MazeController mc;

        public void Run()
        {
            // Configure console properties
            Console.WindowHeight = 40;
            Console.WindowWidth = 100;
            Console.SetBufferSize(200, 80);

            mc = new MazeController();
            mc.randomizeMaze();
            this.DrawScreen(ref mc);

            int input = 0;
            mc.completeLevel += CompleteLevel;
            while (!this.LevelComplete)
            {
                // Get movement from user
                input = GetMovement();
                
                // Update game state based on current input
                UpdateGameState(ref mc, input);

                // Redraw the screen to reflect the updated gamestate
                this.DrawScreen(ref mc);
            }
        }

        private void UpdateGameState(ref MazeController mc, int input)
        {
            mc.gameTick(input);
            while( (mc.mazePlayer.getSubPos()[0] != 0) 
                || (mc.mazePlayer.getSubPos()[1] != 0) )
               mc.gameTick(input);
        }

        private int GetMovement()
        {
            int input = 0;
            var s = System.Console.ReadKey();
            switch (s.Key)
            {
                case (ConsoleKey.LeftArrow):
                    input = 3;
                    break;
                case (ConsoleKey.RightArrow):
                    input = 4;
                    break;
                case (ConsoleKey.UpArrow):
                    input = 1;
                    break;
                case (ConsoleKey.DownArrow):
                    input = 2;
                    break;
                default:
                    input = 0;
                    break;
            }
            return input;
        }

        private void CompleteLevel()
        {
            // this.LevelComplete = true;
            mc.randomizeMaze();
            Console.Clear();
        }


        private void DrawScreen(ref MazeController m)
        {
            System.Console.Clear();

            Console.WriteLine("Level: {0}   Score: {1}   Ticks: {2}", m.level, m.mazePlayer.score, m.ticks);

            for (int i = 0; i != m.mazeMaze.getHeight(); i++)
            {
                // núna er verið að teikna línu nr. i

                for (int j = 0; j != m.mazeMaze.getWidth(); j++)
                {
                    bool cellFilled = false;
                    // núna er verið að teikna sellu númer j í línun i
                    // þ.e. sellu i,j

                    // þetta er geggjaðslega inefficient. Núna þarf ég að fara gegnum alla hluti
                    // og athuga hvort sá hlutur sé á þessari sellu.
                    foreach (MazeTreasure treasure in m.mazeTreasures)
                    {
                        if ((treasure.pos[0] == i) && (treasure.pos[1] == j))
                        {
                            System.Console.Write("t");
                            cellFilled = true;
                        }
                    }
                    if (cellFilled)
                        continue;

                    if ((m.mazePlayer.pos[0] == i) && (m.mazePlayer.pos[1] == j))
                    {
                        System.Console.Write("@");
                        cellFilled = true;
                    }
                    if (cellFilled)
                        continue;

                    foreach (MazeObject end in m.mazeEnds)
                    {
                        if ((end.pos[0] == i) && (end.pos[1] == j))
                        {
                            System.Console.Write("§");
                            cellFilled = true;
                        }
                    }
                    if (cellFilled)
                        continue;

                    // ef ekkert object er í sellu i,j þá er teiknaður veggur (#) eða gólf (.)
                    if (m.mazeMaze.getCellAt(i, j) == 1)
                    {
                        Console.Write("#");
                    }
                    else
                    {
                        Console.Write(".");
                    }
                }
                // Kominn á endann á línunni og fer niður í næstu línu
                Console.WriteLine("");
            }

            Console.WriteLine("Use the arrow keys!");
        }
    }
}
